package com.shiro.sso.demo.config;

import com.shiro.sso.demo.filter.AuthFilter;
import com.shiro.sso.demo.filter.LogoutFilter;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.Filter;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;


@Configuration
public class ShiroConfig {


    @Bean
    @DependsOn("authProperties")
    public AuthFilter authFilter() {
        return new AuthFilter();
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilter() {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(new DefaultWebSecurityManager());
        shiroFilter.setLoginUrl("/login");

        Map<String, String> filterMap = new LinkedHashMap<String, String>();
        filterMap.put("/login", "anon");
        filterMap.put("/logout", "logout");
        filterMap.put("/**", "authc");
        shiroFilter.setFilterChainDefinitionMap(filterMap);

        Map<String, Filter> customFilterMap = new LinkedHashMap<String, Filter>();
        customFilterMap.put("authc", authFilter());
        customFilterMap.put("logout", new LogoutFilter());
        shiroFilter.setFilters(customFilterMap);

        return shiroFilter;
    }

}
